package com.lzy.pattern.proxy.jdk_proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @ClassName: ProxyFactory
 * @Description: 代理工厂
 * @Author: LiuZy
 * @Date: 2022/4/19 21:09
 * @Version: 1.0
 */
public class ProxyFactory {

    private TrainStation trainStation = new TrainStation();

    public SellTickets getProxyObject(){

        SellTickets proxy = (SellTickets)Proxy.newProxyInstance(
                trainStation.getClass().getClassLoader(),
                trainStation.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        if("sell".equals(method.getName())){
                            System.out.println("代理点收取了一些手续费（JDK动态代理）");
                        }

                        if(args != null){
                            for (Object org : args) {
                                System.out.print(org.toString() + " ");
                            }
                        }
                        System.out.println();
                        Object result = method.invoke(trainStation, args);
                        return result;
                    }
                }
        );
        return proxy;
    }
}